import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/home/user_detail_entity.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/expert_api.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/header_delegate.dart';

import '../../resources/colours.dart';
import '../../utils/utils.dart';
import '../../widgets/base/qkd_text.dart';

class ExpertLeaguesPage extends StatefulWidget {
  const ExpertLeaguesPage({super.key});

  @override
  State<ExpertLeaguesPage> createState() => _ExpertLeaguesPageState();
}

class _ExpertLeaguesPageState extends State<ExpertLeaguesPage> {
  UserDetailEntity? user = Get.arguments;
  List<LeagueTop>? data;
  List<ExpertLeagueItem>? items;
  List<String> titles = ["联赛","发文数","命中率"];

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async{
    data = await ExpertApi.getExpertLeagues(user?.userId ?? "");
    items = data?.map((e) => ExpertLeagueItem.from(user,e)).toList() ?? [];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: Styles.appBar(
        backgroundColor: Colours.transparent,
        systemUiOverlayStyle: const SystemUiOverlayStyle(
          statusBarIconBrightness: Brightness.light,
        ),
        leadingColor: Colors.white,
        title: QkdText("联赛分布",color: Colors.white)
      ),
      body: Stack(
        children: [
          Positioned(
            top: 0,left: 0,right: 0,
            child: Image.asset(Utils.getImgPath("user_header_bg.png"),fit: BoxFit.fill)),
          Positioned.fill(
            top: MediaQuery.of(context).padding.top+44,
            child: Container(
              clipBehavior: Clip.antiAlias,
              decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.vertical(top: Radius.circular(8))
              ),
              child: CustomScrollView(
                slivers: [
                  SliverPersistentHeader(
                    pinned: true,
                    delegate: HeaderDelegate(
                      child: Container(
                        color: Colors.white,
                        height: 40,
                        margin: EdgeInsets.symmetric(horizontal: 26),
                        child: Row(
                          children: titles.map<Widget>((e) => Expanded(
                            child: QkdText(
                              e,color: Colours.grey99,
                              textAlign: e == "发文数"?TextAlign.center:e=="联赛"?TextAlign.left:TextAlign.right,
                            ),
                          )).toList()
                        ),
                      ),
                      extent: 40,
                    )
                  ),
                  SliverList.list(
                    children: items?.map<Widget>((e) => GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: () => Get.toNamed(Routes.expertLeagueList, arguments: e),
                      child: item(e).paddingSymmetric(horizontal: 26),
                    )).toList().insertEvery(
                      const Divider(
                        color: Colours.greyEE,height: 0.5
                      ).paddingSymmetric(horizontal: 16)
                    ) ?? []
                  )
                ],
              )
            ),
          )
        ],
      ),
    );
  }

  Widget item(ExpertLeagueItem? item){
    return DefaultTextStyle(
      style: const TextStyle(fontSize: 14,color: Colours.text),
      child: SizedBox(
        height: 40,
        child: Row(
          children: [
            Expanded(child: QkdText(item?.leagueName ?? "",needCenter: true,).alignStart),
            Expanded(child: QkdText(item?.content ?? "-",needCenter: true).center),
            Expanded(child: QkdText(item?.hitRatio ?? "-",
              needCenter: true,
              fontSize: 20,color: Colours.main,fontFamily: "Din"
            ).alignEnd
            ),
          ]
        ),
      ),
    );
  }
}
